use pid-panel , replace

* Now reshape to go from wide to long 
reshape long trustfcc trustgc trustbt satisdem, i(ID) j(year)

* at least for the time being, drop everything after 2017
drop if year >2017

xtset ID year

* Ok. Our focal variable is trust in FCC. Our treatment is ID with the AfD

gen afdid = 0
replace afdid = 1 if year == 2014 & pid2014 == 1
replace afdid = 1 if year == 2015 & pid2015 == 1
replace afdid = 1 if year == 2016 & pid2016 == 1
replace afdid = 1 if year == 2017 & pid2017 == 1

* Oops. Forgot to carry forward the information about missing values
* Nobody has an AfD PID in 2013, so nothing to do here 
replace afdid = . if pid2014 == . & year == 2014
replace afdid = . if pid2015 == . & year == 2015
replace afdid = . if pid2016 == . & year == 2016
replace afdid = . if pid2017 == . & year == 2017

* Labels
lab var afdid "AfD identification"
lab var trustfcc "Trust FCC"
lab var trustgc "Trust ordinary courts"
lab var trustbt "Trust in parliament"
lab var satisdem "Satisfaction with democracy"
lab var year Year
* lab def afdid 0 "no" 1 yes 
* lab val afdid afdid 

* For robustness, also look at alternative AfD identification (include "other" for 2014/17)
gen altafdid = 0 

replace altafdid = 1 if year == 2014 & altpid2014 == 1
replace altafdid = 1 if year == 2015 & altpid2015 == 1
replace altafdid = 1 if year == 2016 & altpid2016 == 1
replace altafdid = 1 if year == 2017 & altpid2017 == 1

* Carry forward missingness
replace altafdid = . if altpid2014 == . & year == 2014
replace altafdid = . if altpid2015 == . & year == 2015
replace altafdid = . if altpid2016 == . & year == 2016
replace altafdid = . if altpid2017 == . & year == 2017
* lab var altafdid "AfD identification (alt)"
* lab val altafdid afdid

* How many complete obeservations?
preserve
bysort ID: gen complete = ! missing(trustfcc) & ! missing(afdid)
bysort ID: generate numberobs = sum(complete)
bysort ID: drop if _n < _N
tab numberobs
restore 
* How many observed at least twice?
display (4337 -640) / 4337

* Have a closer look at actual patterns
preserve
drop if missing(trustfcc)
drop if missing(afdid)
xtdescribe, patterns(20)
restore
* People who dropped out after the first wave just 7.4 per cent
* People who droped out after the first two wave 8.6 per cent
* People who took only part in the second wave 6 per cent

* Who is non-missing values for the last (2017) wave(s)
preserve
keep if year == 2017
keep if !missing(trustfcc)
keep if !missing(afdid)
d, short
restore
* How many AfD identifiers?
tab year afdid,row
xttrans afdid

* Variation of trust in FCC
summ trustfcc, det
tabstat trustfcc,by(year)
tabstat trustfcc,by(afdid)
xtsum trustfcc
* Here is a fixed effects model that actually works, even with robust errors  
* xtreg trustfcc ib2015.year i.afdid, fe vce(cluster ID)
* est store xtregtimeonly

* Now run interaction with time 
xtreg trustfcc i.afdid##ib2015.year, fe vce(cluster ID)
est store xtreginteract



* And now a dynamic model with time interactions 
* xtdpdml trustfcc, pre(afdid) tfix xfree(afdid) fiml vce(robust)
* est store dynamic

esttab xtreginteract using table-1.tex ,label wide se nobaselevels note(Standard errors are cluster-robust) alignment(SS) booktabs noomitted nonum replace

* set plotplain scheme
set scheme plotplain

* Plot from second model 
margins, at(year=(2013 2014 2015 2016 2017) afdid=(0 1))

marginsplot, ytitle("Predicted trust in FCC") recast(scatter) 
graph export xt-interactive-margins.pdf, replace

* Robustness check 1: results do not depend on including data from 2017
xtreg trustfcc i.afdid##ib2015.year if year <2017, fe vce(cluster ID)
est store robust1
esttab robust1 using table-robust-1.tex ,label wide se nobaselevels note(Standard errors are cluster-robust) alignment(SS) booktabs noomitted nonum replace
margins, at(year=(2013 2014 2015 2016) afdid=(0 1))
marginsplot, ytitle("Predicted trust in FCC") recast(scatter)
graph export xt-interactive-margins-robust-1.pdf, replace

* Robustness check 2: use alternative operationalisation for AfD ID 
* Results are virtually identical 
* xtreg trustfcc ib2015.year i.altafdid, fe vce(cluster ID)
* est store altxtregtimeonly

xtreg trustfcc i.altafdid##ib2015.year, fe vce(cluster ID)
est store altxtreginteract
esttab altxtreginteract using table-robust-2.tex ,label wide se nobaselevels note(Standard errors are cluster-robust) alignment(SS) booktabs noomitted nonum replace
margins, at(year=(2013 2014 2015 2016 2017) altafdid=(0 1))
marginsplot, ytitle("Predicted trust in FCC") recast(scatter)
graph export xt-interactive-margins-robust-2.pdf, replace



* Robustness check 3: look at trust in ordinary courts - also very similar results
* (no info for 2013)
xtreg trustgc i.afdid##ib2015.year, fe vce(cluster ID)
est store xtgc
esttab xtgc using table-robust-3.tex ,label wide se nobaselevels note(Standard errors are cluster-robust) alignment(SS) booktabs noomitted nonum replace
margins, at(year=(2014 2015 2016 2017) afdid=(0 1))
marginsplot, ytitle("Predicted trust in ordinary courts") recast(scatter)
graph export xt-interactive-margins-robust-3.pdf, replace



* Robustness check 4: look at trust in Bundesatg: again, the same pattern
xtreg trustbt i.afdid##ib2015.year, fe vce(cluster ID)
est store xtbt
esttab xtbt using table-robust-4.tex ,label wide se nobaselevels note(Standard errors are cluster-robust) alignment(SS) booktabs noomitted nonum replace
margins, at(year=(2013 2014 2015 2016 2017) afdid=(0 1))
marginsplot, ytitle("Predicted trust in parliament") recast(scatter)
graph export xt-interactive-margins-robust-4.pdf, replace


* Robustness check 5: look at satisfaction with democracy: same 
xtreg satisdem i.afdid##ib2015.year, fe vce(cluster ID)
est store xtsatisd
esttab xtbt using table-robust-5.tex ,label wide se nobaselevels note(Standard errors are cluster-robust) alignment(SS) booktabs noomitted nonum replace
margins, at(year=(2013 2014 2015 2016 2017) afdid=(0 1))
marginsplot, ytitle("Predicted satisfaction with democracy") recast(scatter)
graph export xt-interactive-margins-robust-5.pdf, replace 


* Finally, the sankey plot

gen pid2013=0
* ssc install alluvial


* make missing a category
recode pid2014 (.=3)
recode pid2015 (.=3)
recode pid2016 (.=3)
recode pid2017 (.=3)

* define common label 
lab define mypid 1 "afd" 0 "other" 3 "missing"

lab val pid2013 mypid
lab val pid2014 mypid
lab val pid2015 mypid
lab val pid2016 mypid
lab val pid2017 mypid

* redefine var labels
lab var pid2013 "2013"
lab var pid2014 "2014"
lab var pid2015 "2015"
lab var pid2016 "2016"
lab var pid2017 "2017"

* Plot
alluvial pid2013 pid2014 pid2015 pid2016 pid2017, palette(pastel)
graph export afd-alluvial.pdf, replace
